package com.ybkj.daijia.config;

import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import com.ybkj.daijia.Setting;
import com.ybkj.daijia.serverUtils.SettingUtils;
import com.ybkj.daijia.SignHelper;
import com.ybkj.daijia.common.Principal;
import com.ybkj.daijia.common.Result;
import com.ybkj.daijia.server.mc.Enterprise;
import com.ybkj.daijia.server.mc.Passenger;
import com.ybkj.daijia.server.mc.PassengerAccount;
import com.ybkj.daijia.server.mc.PassengerAccount.PayType;
import com.ybkj.daijia.server.mc.PassengerAddress;
import com.ybkj.daijia.server.mc.PassengerConcats;
import com.ybkj.daijia.server.mc.PassengerGrade;
import com.ybkj.daijia.server.model.Receipt;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.client.RestTemplate;

/**
 * @author Shine
 */
@Component
public class PassengerApi {


    /**
     * 分页查询客户
     */
    public static final String QUERY_PASSENGER = "api/passengers";
    /**
     * 新分页查询客户
     */
    public static final String QUERY_PASSENGER_BY_MULTICONDITION = "api/passengers/findByMultiCondition";
    /**
     * 查询符合条件的乘客条数
     */
    public static final String COUNT_PASSENGER_BY_MULTICONDITION = "api/passengers/countByMultiCondition";
    /**
     * 根据推荐人分页查询客户
     */
    public static final String QUERY_PASSENGER_BY_INTRODUCER = "api/passengers/introducer";
    /**
     * 根据电话号码查询客户
     */
    public static final String QUERY_ALL_PASSENGER_BY_PHONE = "api/passengers/phoneLike";
    /**
     * 新增用户
     */
    public static final String CREATE_PASSENGER = "api/passengers";
    /**
     * 修改用户
     */
    public static final String UPDATE_PASSENGER = "api/passengers/";
    /**
     * 更新用户设备信息
     */
    public static final String UPDATE_PASSENGER_DEVICETYPE = "api/passengers/{passengerId}/deviceType";
    /**
     * 根据id删除单个用户
     */
    public static final String DELETE_PASSENGER = "api/passengers/";
    /**
     * 根据id解除黑名单
     */
    public static final String UNBLACK_PASSENGER = "api/passengers/{passengerId}/inBlackList";
    /**
     * 根据类型、公司查询客户
     */
//	public static final String QUERY_ALL_PASSENGER_BY_TYPE_COMPANY = "api/passengers/type";
    /**
     * 根据id查询单个用户
     */
    public static final String ONE_PASSENGER = "api/passengers/";
    /**
     * 根据id查询单个用户等级
     */
    public static final String ONE_PASSENGER_GRADE = "api/grades/";
    /**
     * 根据电话查询单个用户
     */
    public static final String ONE_PASSENGER_PHONE_EMAIL_IDCARD = "api/passengers/findone";
    /**
     * 多条件查询财务记录
     */
    public static final String QUERY_COMPANY_ACCOUNT = "api/passengerAccount";
    /**
     * 分页查询客户财务记录
     */
    public static final String QUERY_PASSENGER_ACCOUNT = "api/passengerAccount/{passengerId}/passengerAccount";
    /**
     * 添加客户财务记录
     */
    public static final String CREATE_PASSENGER_ACCOUNT = "api/passengers/{passengerId}/passengerAccountDetail";
    /**
     * 多条件查询账户金额总和
     */
    public static final String SUM_PASSENGER_ACCOUNT_MONEY = "api/passengerAccount/sumAccount";
    /**
     * 多条件查询乘客余额总和
     */
    public static final String SUM_PASSENGER_VIRTUAL = "api/passengers/sumDrawMoney";
    /**
     * 修改客户开票金额
     */
    public static final String UPDATE_PASSENGER_RECEIPT = "api/passengers/{passengerId}/receiptMoney";
    /**
     * 查询客户来源
     */
    public static final String QUERY_ALL_PASSENGER_CHANNEL = "api/passengers/channel";
    /**
     * 客户来源统计
     */
    public static final String PASSENGER_CHANNEL = "api/passengers/time";
    /**
     * 根据条件查询客户数量
     */
    public static final String COUNT_PASSENGER = "api/passengers/countByCompany";
    /**
     * 根据订单id等查询账户记录
     */
    public static final String PASSENGER_ACCOUNT_ORDER = "api/passengerAccount/findByOrderId";
    /**
     * 查询常用目的地
     */
    public static final String PASSENGER_TOPLACE = "api/passengers/{passengerId}/addressbooks";
    /**
     * 新增常用目的地
     */
    public static final String CREATE_PASSENGER_TOPLACE = "api/passengers/{passengerId}/addressbooks";
    /**
     * 修改常用目的地
     */
    public static final String UPDATE_PASSENGER_TOPLACE = "api/passengers/{passengerId}/addressbooks/{addressId}";
    /**
     * 修改客户位置信息
     */
    public static final String UPDATE_PASSENGER_FROMPLACE = "api/passengers/{passengerId}/address";
    /**
     * 查询联系人
     */
    public static final String PASSENGER_CONCATS = "api/passengers/{passengerId}/concats";
    /**
     * 新增联系人
     */
    public static final String CREATE_PASSENGER_CONCATS = "api/passengers/{passengerId}/concats";
    /**
     * 修改联系人
     */
    public static final String UPDATE_PASSENGER_CONCATS = "api/passengers/{passengerId}/concats/{concatsId}";
    /**
     * 分页查询企业
     */
    public static final String QUERY_ENTERPRISE = "api/enterprises";
    /**
     * 新分页查询企业
     */
    public static final String QUERY_ENTERPRISE_PARAMS = "api/enterprises/listData";
    /**
     * 新增企业
     */
    public static final String CREATE_ENTERPRISE = "api/enterprises";
    /**
     * 修改企业
     */
    public static final String UPDATE_ENTERPRISE = "api/enterprises/";
    /**
     * 删除企业
     */
    public static final String DELETE_ENTERPRISE = "api/enterprises/";
    /**
     * 批量新增企业用户
     */
    public static final String CREATE_ENTERPRISE_PASSENGERS = "api/enterprises/{enterpriseId}/passengers";
    /**
     * 批量修改企业用户
     */
    public static final String UPDATE_ENTERPRISE_PASSENGERS = "api/enterprises/{enterpriseId}/updatePassengers";
    /**
     * 根据id查询企业
     */
    public static final String ONE_ENTERPRISE = "api/enterprises/";
    /**
     * 分页查询企业财务记录
     */
    public static final String QUERY_ENTERPRISE_ACCOUNT = "api/passengerAccount/{enterpriseId}/accountDetail";
    /**
     * 添加企业财务记录
     */
    public static final String CREATE_ENTERPRISE_ACCOUNT = "api/enterprises/{enterpriseId}/accountDetail";
    /**
     * 分页查询黑名单
     */
    public static final String QUERY_BLACK = "api/passengers/inBlackList";
    /**
     * 查询所有等级
     */
    public static final String QUERY_ALL_GRADES = "api/grades";
    /**
     * 新增等级
     */
    public static final String CREATE_GRADE = "api/grades";
    /**
     * 修改等级
     */
    public static final String UPDATE_GRADE = "api/grades/{gradeId}";
    /**
     * 删除等级
     */
    public static final String DELETE_GRADE = "api/grades/deleteGrade/{gradeId}";
    /**
     * 处理客户等级为空的为指定等级
     */
    public static final String DISPOSE_GRADE = "api/grades/disposeGrade/{gradeId}";
    /**
     * 根据公司id查询等级
     */
    public static final String FINDBYCOMPANY = "api/grades/findByCompanyId";
    /**
     * 修改客户等公司简称
     */
    public static final String CHANGE_ABBREVIATION = "api/passengers/updateCompanyName";
    public static final String BATCHCREATE = "api/passengers/v4/batchCreate";
    /**
     * 查询客户
     */
    public static final String FIND_IDS = "api/passengers/findByIds";
    /***
     * 更新用户的渠道
     */
    public static final String UPDATE_CHANNEL_NAME_P = "passenger/updateChannel/";
    public static final String UPDATE_CHANNEL_NAME_B = "?appKey={appKey}&channelName={channelName}";
    /***
     * 获取用户微信昵称和头像地址
     */
    public static final String GET_WEIXIN_INFOS = "passenger/getWeixinInfos";
    /****
     *  根据推荐类型和推荐人统计被推荐乘客
     */
    public static final String COUNT_PASSENGER_BY_INTRODUCER = "/api/passengers/countPassengerByIntroduce?appKey={appKey}&introduceType={introduceType}&introducer={introducer}";
    private Logger log = LoggerFactory.getLogger(PassengerApi.class);
    @Value("${module.passenger.url}")
    private String passengerBaseURL;
    @Value("${module.passenger.appKey}")
    private String passengerAppKey;
    @Value("${module.passenger.appId}")
    private String passengerAppId;
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private SettingUtils settingUtils;

    public String get(String apiurl, Map<String, String> kv, Setting setting) {

        kv.put("appKey", setting.getSecKey());

        String sign = SignHelper.sign(SignHelper.buildSignMessage(kv), passengerAppKey);

        kv.put("sign", sign);
        kv.put("appId", passengerAppId);
        String result = null;

        try {
            result = restTemplate.getForObject(apiurl, String.class, kv);
        } catch (HttpClientErrorException e) {
            result = e.getResponseBodyAsString();
        } catch (HttpServerErrorException e) {
            result = e.getResponseBodyAsString();
        }
        return result;

    }

    public String post(String apiurl, Map<String, String> kv, Setting setting) {

        kv.put("appKey", setting.getSecKey());

        String sign = SignHelper.sign(SignHelper.buildSignMessage(kv), passengerAppKey);

        kv.put("sign", sign);
        kv.put("appId", passengerAppId);

        String result = null;

        try {
            result = restTemplate.postForObject(apiurl, null, String.class, kv);
        } catch (HttpClientErrorException e) {
            result = e.getResponseBodyAsString();
        } catch (HttpServerErrorException e) {
            result = e.getResponseBodyAsString();
        }

        return result;

    }

    public String post(String apiurl, MultiValueMap<String, String> kv, Setting setting) {
        kv.add("appKey", setting.getSecKey());
        Set<Map.Entry<String, List<String>>> entries = kv.entrySet();
        Map<String, String> params = new HashMap<String, String>();
        for (Map.Entry<String, List<String>> entry : entries) {
            List<String> values = entry.getValue();
            params.put(entry.getKey(), CollectionUtils.isEmpty(values) ? null : values.get(0));
        }
        RestTemplate rest = new RestTemplate();
        String result = null;
        try {
            result = rest.postForObject(apiurl, kv, String.class);
        } catch (HttpClientErrorException e) {
            result = e.getResponseBodyAsString();
        } catch (HttpServerErrorException e) {
            result = e.getResponseBodyAsString();
        }

        return result;
    }

    public String put(String apiurl, Map<String, String> kv, Setting setting) {

        kv.put("appKey", setting.getSecKey());

        String sign = SignHelper.sign(SignHelper.buildSignMessage(kv), passengerAppKey);

        kv.put("sign", sign);
        kv.put("appId", passengerAppId);

        String result = null;

        try {
            restTemplate.put(apiurl, null, kv);
        } catch (HttpClientErrorException e) {
            result = e.getResponseBodyAsString();
        } catch (HttpServerErrorException e) {
            result = e.getResponseBodyAsString();
        }
        return result;

    }

    public String delete(String apiurl, Map<String, String> kv, Setting setting) {

        kv.put("appKey", setting.getSecKey());

        String sign = SignHelper.sign(SignHelper.buildSignMessage(kv), passengerAppKey);

        kv.put("sign", sign);
        kv.put("appId", passengerAppId);

        String result = null;

        try {
            restTemplate.delete(apiurl, kv);
        } catch (HttpClientErrorException e) {
            result = e.getResponseBodyAsString();
        }
        return result;

    }

    public Map<String, Object> updateChannel(Passenger passenger) {
        Map<String, String> paramters = new HashMap<>();
        paramters.put("appKey", passenger.getAppKey());
        paramters.put("channelName", passenger.getChannel_name());
        // 请求头
        // put请求
        ResponseEntity<com.alibaba.fastjson.JSONObject> resultEntity = restTemplate.exchange(
            getPassengerBaseURL() + UPDATE_CHANNEL_NAME_P + passenger.getId()
                + UPDATE_CHANNEL_NAME_B, HttpMethod.PUT, null,
            com.alibaba.fastjson.JSONObject.class, paramters);
        if (null != resultEntity) {
            return resultEntity.getBody();
        }
        return null;
    }

    public com.alibaba.fastjson.JSONArray getWxInfo(Set<Long> ids) {
        Map<String, String> paramters = new HashMap<>();
        String idsStr = "";
        for (Long id : ids) {
            if (null != id) {
                idsStr += id + ",";
            }
        }
        idsStr = idsStr.substring(0, idsStr.length() - 1);
        paramters.put("ids", idsStr);
        String reStr = restTemplate
            .getForObject(getPassengerBaseURL() + GET_WEIXIN_INFOS + "?ids={ids}", String.class,
                paramters);
        if (null != reStr) {
            com.alibaba.fastjson.JSONObject json = JSON.parseObject(reStr);
            if (null != json) {
                Boolean success = json.getBoolean("success");
                if (success) {
                    com.alibaba.fastjson.JSONArray data = json.getJSONArray("data");
                    return data;
                } else {
                    return null;
                }
            }
        }
        return null;
    }

    /***
     * zhyu
     * @return
     */
    public Long countPassengerByIntroducer(String introducer, Long companyId,
        Integer introduceType) {

        // 增加财务记录
        StringBuilder sb = new StringBuilder();

        Map<String, String> kv = new HashMap<String, String>();

        sb.append(getPassengerBaseURL() + PassengerApi.COUNT_PASSENGER_BY_INTRODUCER);

        kv = new HashMap<String, String>();

        kv.put("companyId", null == companyId ? "null" : String.valueOf(companyId));
        kv.put("appKey", settingUtils.get().getSecKey());
        kv.put("introducer", introducer);
        kv.put("introduceType", String.valueOf(introduceType));

        String resultStr = restTemplate.getForObject(sb.toString(), String.class, kv);
        String error = null;
        if (null != resultStr) {
            Long count = null;
            JSONObject jo = new JSONObject(resultStr);
            Integer code = jo.getInt("code");
            if (code == 0) {
                count = jo.getLong("data");
                return count;
            } else {
                error = jo.getString("message");
            }
        }
        log.info("查询 mc 失败 失败信息{} ", error);
        return null;
    }


    public Result<Passenger> createPersonal(Passenger passenger, Setting setting) {
        Result<Passenger> result = new Result<Passenger>();
        result.setSuccess(false);
        try {

            StringBuilder sb = new StringBuilder();
            Map<String, String> kv = new HashMap<String, String>();

            sb.append(getPassengerBaseURL()
                + PassengerApi.CREATE_PASSENGER
                + ".json?appKey={appKey}&appId={appId}&sign={sign}&phone={phone}&companyId={companyId}&companyName={companyName}");

            kv.put("phone", String.valueOf(passenger.getPhone()));
            kv.put("companyId", String.valueOf(passenger.getCompanyId()));
            kv.put("companyName", String.valueOf(passenger.getCompanyName()));
            if (StringUtils.isNotBlank(passenger.getIdcard())) {
                kv.put("idcard", String.valueOf(passenger.getIdcard()));
                sb.append("&idcard={idcard}");
            }
            if (StringUtils.isNotBlank(passenger.getEmail())) {
                kv.put("email", String.valueOf(passenger.getEmail()));
                sb.append("&email={email}");
            }
            if (StringUtils.isNotBlank(passenger.getName())) {
                kv.put("name", String.valueOf(passenger.getName()));
                sb.append("&name={name}");
            }
            if (StringUtils.isNotBlank(passenger.getChannel_name())) {
                kv.put("channel_name", String.valueOf(passenger.getChannel_name()));
                sb.append("&channel_name={channel_name}");
            }
            if (StringUtils.isNotBlank(passenger.getIntroducer())) {
                kv.put("introducer", String.valueOf(passenger.getIntroducer()));
                sb.append("&introducer={introducer}");
            }
            if (null != passenger.getIntroduceType()) {
                kv.put("introduceType",
                    String.valueOf(passenger.getIntroduceType()));
                sb.append("&introduceType={introduceType}");
            }
            if (StringUtils.isNotBlank(passenger.getAddress())) {
                kv.put("address", String.valueOf(passenger.getAddress()));
                sb.append("&address={address}");
            }
            if (StringUtils.isNotBlank(passenger.getPlateNumber())) {
                kv.put("plateNumber", String.valueOf(passenger.getPlateNumber()));
                sb.append("&plateNumber={plateNumber}");
            }
            if (null != passenger.getCompanyId()) {
                kv.put("companyId", String.valueOf(passenger.getCompanyId()));
                sb.append("&companyId={companyId}");
            }
            if (StringUtils.isNotBlank(passenger.getCompanyName())) {
                kv.put("companyName", String.valueOf(passenger.getCompanyName()));
                sb.append("&companyName={companyName}");
            }
            if (StringUtils.isNotBlank(passenger.getMemo())) {
                kv.put("memo", String.valueOf(passenger.getMemo()));
                sb.append("&memo={memo}");
            }

            kv.put("allowOverdraw", String.valueOf(passenger.isAllowOverdraw()));
            sb.append("&allowOverdraw={allowOverdraw}");

            if (null != passenger.getGender()) {
                kv.put("gender", String.valueOf(passenger.getGender()));
                sb.append("&gender={gender}");
            }

            if (null != passenger.getGrade_id()) {
                kv.put("grade_id", String.valueOf(passenger.getGrade_id()));
                sb.append("&grade_id={grade_id}");
            }

            if (StringUtils.isNotBlank(passenger.getPassword())) {
                kv.put("password", String.valueOf(passenger.getPassword()));
                sb.append("&password={password}");
            }
            if (StringUtils.isNotBlank(passenger.getWxHeadImgUrl())) {
                kv.put("wxHeadImgUrl", String.valueOf(passenger.getWxHeadImgUrl()));
                sb.append("&wxHeadImgUrl={wxHeadImgUrl}");
            }
            if (StringUtils.isNotBlank(passenger.getWxNickName())) {
                kv.put("wxNickName", String.valueOf(passenger.getWxNickName()));
                sb.append("&wxNickName={wxNickName}");
            }

            String resultStr = post(sb.toString(), kv, setting);
            if (null != resultStr) {
                JSONObject jo = new JSONObject(resultStr);
                Integer code = jo.getInt("code");
                if (code == 0) {
                    JSONObject data = jo.getJSONObject("data");
                    Gson gson = new GsonBuilder().setDateFormat(com.ybkj.daijia.constant.DateConstant.DATE_TIME_FORMATE).create();
                    Passenger p = gson.fromJson(data.toString(), Passenger.class);
                    result.setResult(p);
                    result.setSuccess(true);
                } else {
                    String message = jo.getString("message");
                    result.setErrorcode(String.valueOf(code));
                    result.setMessage(message);
                    result.setSuccess(false);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public Result<Passenger> updatePersonal(Passenger passenger, Setting setting) {

        Result<Passenger> result = new Result<Passenger>();
        result.setSuccess(false);

        StringBuilder sb = new StringBuilder();
        Map<String, String> kv = new HashMap<String, String>();

        sb.append(getPassengerBaseURL()
            + PassengerApi.UPDATE_PASSENGER
            + passenger.getId()
            + "?appKey={appKey}&appId={appId}&sign={sign}&phone={phone}&companyId={companyId}&companyName={companyName}");

        kv.put("phone", String.valueOf(passenger.getPhone()));
        kv.put("companyId", String.valueOf(passenger.getCompanyId()));
        kv.put("companyName", String.valueOf(passenger.getCompanyName()));
        if (StringUtils.isNotBlank(passenger.getIdcard())) {
            kv.put("idcard", String.valueOf(passenger.getIdcard()));
            sb.append("&idcard={idcard}");
        }
        if (StringUtils.isNotBlank(passenger.getEmail())) {
            kv.put("email", String.valueOf(passenger.getEmail()));
            sb.append("&email={email}");
        }
        if (StringUtils.isNotBlank(passenger.getName())) {
            kv.put("name", String.valueOf(passenger.getName()));
            sb.append("&name={name}");
        }
        if (StringUtils.isNotBlank(passenger.getChannel_name())) {
            kv.put("channel_name", String.valueOf(passenger.getChannel_name()));
            sb.append("&channel_name={channel_name}");
        }
        if (StringUtils.isNotBlank(passenger.getIntroducer())) {
            kv.put("introducer", String.valueOf(passenger.getIntroducer()));
            sb.append("&introducer={introducer}");
        }
        if (null != passenger.getIntroduceType()) {
            kv.put("introduceType",
                String.valueOf(passenger.getIntroduceType()));
            sb.append("&introduceType={introduceType}");
        }
        if (StringUtils.isNotBlank(passenger.getAddress())) {
            kv.put("address", String.valueOf(passenger.getAddress()));
            sb.append("&address={address}");
        }
        if (StringUtils.isNotBlank(passenger.getPlateNumber())) {
            kv.put("plateNumber", String.valueOf(passenger.getPlateNumber()));
            sb.append("&plateNumber={plateNumber}");
        }
        if (StringUtils.isNotBlank(passenger.getPhoto())) {
            kv.put("photo", String.valueOf(passenger.getPhoto()));
            sb.append("&photo={photo}");
        }
        if (StringUtils.isNotBlank(passenger.getMemo())) {
            kv.put("memo", String.valueOf(passenger.getMemo()));
            sb.append("&memo={memo}");
        }

        kv.put("allowOverdraw", String.valueOf(passenger.isAllowOverdraw()));
        sb.append("&allowOverdraw={allowOverdraw}");

        if (null != passenger.getGender()) {
            kv.put("gender", String.valueOf(passenger.getGender()));
            sb.append("&gender={gender}");
        }

        if (null != passenger.getGrade_id()) {
            kv.put("grade_id", String.valueOf(passenger.getGrade_id()));
            sb.append("&grade_id={grade_id}");
        }

        if (null != passenger.getDrivingNumberTimes()) {
            kv.put("drivingNumberTimes", String.valueOf(passenger.getDrivingNumberTimes()));
            sb.append("&drivingNumberTimes={drivingNumberTimes}");
        }

        if (null != passenger.getMileage()) {
            kv.put("mileage", String.valueOf(passenger.getMileage()));
            sb.append("&mileage={mileage}");
        }
        if (null != passenger.getUpStatus()) {
            kv.put("upStatus", String.valueOf(passenger.getUpStatus()));
            sb.append("&upStatus={upStatus}");
        }
        if (StringUtils.isNotBlank(passenger.getPassword())) {
            kv.put("password", String.valueOf(passenger.getPassword()));
            sb.append("&password={password}");
        }
        if (StringUtils.isNotBlank(passenger.getIdCardPhoto())) {
            kv.put("idCardPhoto", String.valueOf(passenger.getIdCardPhoto()));
            sb.append("&idCardPhoto={idCardPhoto}");
        }
        if (StringUtils.isNotBlank(passenger.getIdCardBackPhoto())) {
            kv.put("idCardBackPhoto", String.valueOf(passenger.getIdCardBackPhoto()));
            sb.append("&idCardBackPhoto={idCardBackPhoto}");
        }

        String resultStr = put(sb.toString(), kv, setting);
        if (null != resultStr) {
            JSONObject jo = new JSONObject(resultStr);
            String message = jo.getString("message");
            result.setMessage(message);
            result.setSuccess(false);
        } else {
            result.setSuccess(true);
        }

        return result;

    }

    public Result<Enterprise> createEnterprise(StringBuilder sb, Map<String, String> kv,
        Enterprise enterprise, Setting setting) {

        Result<Enterprise> result = new Result<Enterprise>();
        result.setSuccess(false);

        sb.append(getPassengerBaseURL()
            + PassengerApi.CREATE_ENTERPRISE
            + "?appKey={appKey}&appId={appId}&sign={sign}&name={name}&companyId={companyId}&companyName={companyName}");

        kv.put("name", String.valueOf(enterprise.getName()));
        kv.put("companyId", String.valueOf(enterprise.getCompanyId()));
        kv.put("companyName", String.valueOf(enterprise.getCompanyName()));
        if (StringUtils.isNotBlank(enterprise.getAddress())) {
            kv.put("address", String.valueOf(enterprise.getAddress()));
            sb.append("&address={address}");
        }
        if (StringUtils.isNotBlank(enterprise.getMemo())) {
            kv.put("memo", String.valueOf(enterprise.getMemo()));
            sb.append("&memo={memo}");
        }

        kv.put("allowOverdraw", String.valueOf(enterprise.isAllowOverdraw()));
        sb.append("&allowOverdraw={allowOverdraw}");

        if (null != enterprise.getGrade_id()) {
            kv.put("grade_id", String.valueOf(enterprise.getGrade_id()));
            sb.append("&grade_id={grade_id}");
        }

        String resultStr = post(sb.toString(), kv, setting);

        if (null != resultStr) {
            JSONObject jo = new JSONObject(resultStr);
            Integer code = jo.getInt("code");
            if (code == 0) {
                JSONObject data = jo.getJSONObject("data");
                Gson gson = new GsonBuilder().setDateFormat(com.ybkj.daijia.constant.DateConstant.DATE_TIME_FORMATE).create();
                Enterprise e = gson.fromJson(data.toString(), Enterprise.class);

                StringBuilder sb2 = new StringBuilder();
                String api = PassengerApi.CREATE_ENTERPRISE_PASSENGERS;
                api = api.replaceAll("\\{enterpriseId\\}", String.valueOf(e.getId()));
                sb2.append(getPassengerBaseURL() + api
                    + "?appKey={appKey}&appId={appId}&sign={sign}&passengers={passengers}");

                Map<String, String> kv2 = new HashMap<String, String>();

                List<Passenger> passengerlist = enterprise.getPassengers();
                Gson passengerGson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss")
                    .create();
                String passengers = passengerGson.toJson(passengerlist);

                kv2.put("passengers", String.valueOf(passengers));

                resultStr = post(sb2.toString(), kv2, setting);

            }
        }

        if (null != resultStr) {
            JSONObject jo = new JSONObject(resultStr);
            Integer code = jo.getInt("code");
            if (code == 0) {
                JSONObject data = jo.getJSONObject("data");
                Gson gson = new GsonBuilder().setDateFormat(com.ybkj.daijia.constant.DateConstant.DATE_TIME_FORMATE).create();
                Enterprise e = gson.fromJson(data.toString(), Enterprise.class);

                result.setSuccess(true);
                result.setResult(e);
            } else {
                String message = jo.getString("message");
                result.setMessage(message);
                result.setSuccess(false);
            }
        }
        return result;

    }

    public Result<Enterprise> updateEnterprise(Enterprise enterprise, Setting setting) {

        StringBuilder sb = new StringBuilder();
        Map<String, String> kv = new HashMap<String, String>();

        Result<Enterprise> result = new Result<Enterprise>();
        result.setSuccess(false);

        sb.append(getPassengerBaseURL()
            + PassengerApi.UPDATE_ENTERPRISE
            + enterprise.getId()
            + "?appKey={appKey}&appId={appId}&sign={sign}&companyId={companyId}&companyName={companyName}");

        if (StringUtils.isNotBlank(enterprise.getName())) {
            kv.put("name", String.valueOf(enterprise.getName()));
            sb.append("&name={name}");
        }

        kv.put("companyId", String.valueOf(enterprise.getCompanyId()));
        kv.put("companyName", String.valueOf(enterprise.getCompanyName()));

        if (StringUtils.isNotBlank(enterprise.getAddress())) {
            kv.put("address", String.valueOf(enterprise.getAddress()));
            sb.append("&address={address}");
        }

        if (StringUtils.isNotBlank(enterprise.getMemo())) {
            kv.put("memo", String.valueOf(enterprise.getMemo()));
            sb.append("&memo={memo}");
        }

        kv.put("allowOverdraw", String.valueOf(enterprise.isAllowOverdraw()));
        sb.append("&allowOverdraw={allowOverdraw}");

        if (null != enterprise.getGrade_id()) {
            kv.put("grade_id", String.valueOf(enterprise.getGrade_id()));
            sb.append("&grade_id={grade_id}");
        }

        if (null != enterprise.getDrivingNumberTimes()) {
            kv.put("drivingNumberTimes", String.valueOf(enterprise.getDrivingNumberTimes()));
            sb.append("&drivingNumberTimes={drivingNumberTimes}");
        }

        String resultStr = put(sb.toString(), kv, setting);

        if (StringUtils.isBlank(resultStr)) {

            StringBuilder sb2 = new StringBuilder();
            String api = PassengerApi.UPDATE_ENTERPRISE_PASSENGERS;
            api = api.replaceAll("\\{enterpriseId\\}", String.valueOf(enterprise.getId()));
            sb2.append(getPassengerBaseURL()).append(api);

            List<Passenger> passengerlist = enterprise.getPassengers();
            Gson passengerGson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
            String passengers = passengerGson.toJson(passengerlist);

            MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>();
            params.add("passengers", passengers);

            Map<String, String> kv2 = new HashMap<String, String>();
            kv2.put("passengers", passengers);

            kv2.put("appKey", setting.getSecKey());
            String sign = SignHelper.sign(SignHelper.buildSignMessage(kv2), passengerAppKey);

            params.add("appKey", setting.getSecKey());
            params.add("sign", sign);
            params.add("appId", passengerAppId);

            try {
                restTemplate.put(sb2.toString(), params);
            } catch (HttpClientErrorException e) {
                e.printStackTrace();
                resultStr = e.getResponseBodyAsString();
            } catch (HttpServerErrorException e) {
                e.printStackTrace();
                resultStr = e.getResponseBodyAsString();
            }

        }

        if (StringUtils.isNotBlank(resultStr)) {
            JSONObject jo = new JSONObject(resultStr);
            String message = jo.getString("message");
            result.setMessage(message);
            result.setSuccess(false);
        } else {
            result.setSuccess(true);
        }

        return result;

    }

    public Result<Enterprise> updateEnterpriseInfo(Enterprise enterprise, Setting setting) {

        StringBuilder sb = new StringBuilder();
        Map<String, String> kv = new HashMap<String, String>();

        Result<Enterprise> result = new Result<Enterprise>();
        result.setSuccess(false);

        sb.append(getPassengerBaseURL()
            + PassengerApi.UPDATE_ENTERPRISE
            + enterprise.getId()
            + "?appKey={appKey}&appId={appId}&sign={sign}&companyId={companyId}&companyName={companyName}");

        if (StringUtils.isNotBlank(enterprise.getName())) {
            kv.put("name", String.valueOf(enterprise.getName()));
            sb.append("&name={name}");
        }

        kv.put("companyId", String.valueOf(enterprise.getCompanyId()));
        kv.put("companyName", String.valueOf(enterprise.getCompanyName()));

        if (StringUtils.isNotBlank(enterprise.getAddress())) {
            kv.put("address", String.valueOf(enterprise.getAddress()));
            sb.append("&address={address}");
        }

        if (StringUtils.isNotBlank(enterprise.getMemo())) {
            kv.put("memo", String.valueOf(enterprise.getMemo()));
            sb.append("&memo={memo}");
        }

        kv.put("allowOverdraw", String.valueOf(enterprise.isAllowOverdraw()));
        sb.append("&allowOverdraw={allowOverdraw}");

        if (null != enterprise.getGrade_id()) {
            kv.put("gradeId", String.valueOf(enterprise.getGrade_id()));
            sb.append("&gradeId={gradeId}");
        }

        if (null != enterprise.getDrivingNumberTimes()) {
            kv.put("drivingNumberTimes", String.valueOf(enterprise.getDrivingNumberTimes()));
            sb.append("&drivingNumberTimes={drivingNumberTimes}");
        }

        String resultStr = put(sb.toString(), kv, setting);

        if (StringUtils.isNotBlank(resultStr)) {
            JSONObject jo = new JSONObject(resultStr);
            String message = jo.getString("message");
            result.setMessage(message);
            result.setSuccess(false);
        } else {
            result.setSuccess(true);
        }

        return result;

    }

    public Result<Passenger> findOnePassenger(Long id, Setting setting) {
        Result<Passenger> result = new Result<Passenger>();
        result.setSuccess(false);
        Passenger passenger = null;
        StringBuilder sb = new StringBuilder();
        sb.append(getPassengerBaseURL() + PassengerApi.ONE_PASSENGER + id
            + "?appKey={appKey}&appId={appId}&sign={sign}");

        Map<String, String> kv = new HashMap<String, String>();
        String resultStr = get(sb.toString(), kv, setting);
        try {
            if (null != resultStr) {
                JSONObject jo = new JSONObject(resultStr);
                Integer code = jo.getInt("code");
                if (code == 0) {
                    Object object = jo.get("data");
                    if (null != object && !object.toString().equals("null")) {
                        JSONObject data = jo.getJSONObject("data");
                        Gson gson = new GsonBuilder().setDateFormat(com.ybkj.daijia.constant.DateConstant.DATE_TIME_FORMATE).create();
                        passenger = gson.fromJson(data.toString(), Passenger.class);
                        result.setResult(passenger);
                    }
                    result.setSuccess(true);
                } else {
                    result.setSuccess(false);
                    result.setMessage(jo.getString("message"));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public Result<Enterprise> findOneEnterprise(Long id, Setting setting) {

        Result<Enterprise> result = new Result<Enterprise>();
        result.setSuccess(false);

        Enterprise enterprise = null;

        StringBuilder sb = new StringBuilder();
        sb.append(getPassengerBaseURL() + PassengerApi.ONE_ENTERPRISE + id
            + "?appKey={appKey}&appId={appId}&sign={sign}");

        Map<String, String> kv = new HashMap<String, String>();

        String resultStr = get(sb.toString(), kv, setting);

        if (null != resultStr) {
            JSONObject jo = new JSONObject(resultStr);
            Integer code = jo.getInt("code");
            if (code == 0) {
                Object object = jo.get("data");
                if (null != object && !object.toString().equals("null")) {
                    JSONObject data = jo.getJSONObject("data");
                    Gson gson = new GsonBuilder().setDateFormat(com.ybkj.daijia.constant.DateConstant.DATE_TIME_FORMATE).create();
                    enterprise = gson.fromJson(data.toString(), Enterprise.class);
                    result.setResult(enterprise);
                }
                result.setSuccess(true);
            } else {
                result.setSuccess(false);
                result.setMessage(jo.getString("message"));
            }
        }
        return result;
    }

    public Result<Passenger> findOnePassengerByPhone(String phone,
        Setting setting) {
        Result<Passenger> result = new Result<Passenger>();
        try {

            result.setSuccess(false);

            Passenger passenger = null;

            StringBuilder sb = new StringBuilder();
            sb.append(getPassengerBaseURL()
                + PassengerApi.ONE_PASSENGER_PHONE_EMAIL_IDCARD
                + "?appKey={appKey}&appId={appId}&sign={sign}&phone={phone}");

            Map<String, String> kv = new HashMap<String, String>();
            phone = phone.replaceAll(" ", "");
            phone = phone.replaceAll("-", "");
            kv.put("phone", String.valueOf(phone));

            String resultStr = get(sb.toString(), kv, setting);
            if (null != resultStr) {
                JSONObject jo = new JSONObject(resultStr);
                Integer code = jo.getInt("code");
                if (code == 0) {
                    Object object = jo.get("data");
                    if (null != object && !object.toString().equals("null")) {
                        JSONObject data = jo.getJSONObject("data");
                        Gson gson = new GsonBuilder().setDateFormat(com.ybkj.daijia.constant.DateConstant.DATE_TIME_FORMATE).create();
                        passenger = gson.fromJson(data.toString(), Passenger.class);
                        result.setResult(passenger);
                    }
                    result.setSuccess(true);
                } else {
                    result.setSuccess(false);
                    result.setMessage(jo.getString("message"));
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public Result<Passenger> findOnePassengerByEmail(String email,
        Setting setting) {

        Result<Passenger> result = new Result<Passenger>();
        result.setSuccess(false);

        Passenger passenger = null;

        StringBuilder sb = new StringBuilder();
        sb.append(getPassengerBaseURL()
            + PassengerApi.ONE_PASSENGER_PHONE_EMAIL_IDCARD
            + "?appKey={appKey}&appId={appId}&sign={sign}&email={email}");

        Map<String, String> kv = new HashMap<String, String>();
        kv.put("email", String.valueOf(email));

        String resultStr = get(sb.toString(), kv, setting);

        if (null != resultStr) {
            JSONObject jo = new JSONObject(resultStr);
            Integer code = jo.getInt("code");
            if (code == 0) {
                Object object = jo.get("data");
                if (null != object && !object.toString().equals("null")) {
                    JSONObject data = jo.getJSONObject("data");
                    Gson gson = new GsonBuilder().setDateFormat(com.ybkj.daijia.constant.DateConstant.DATE_TIME_FORMATE).create();
                    passenger = gson.fromJson(data.toString(), Passenger.class);
                    result.setResult(passenger);
                }
                result.setSuccess(true);
            } else {
                result.setSuccess(false);
                result.setMessage(jo.getString("message"));
            }
        }
        return result;

    }

    public Result<Passenger> findOnePassengerByIdcard(String idcard,
        Setting setting) {

        Result<Passenger> result = new Result<Passenger>();
        result.setSuccess(false);

        Passenger passenger = null;

        StringBuilder sb = new StringBuilder();
        sb.append(getPassengerBaseURL()
            + PassengerApi.ONE_PASSENGER_PHONE_EMAIL_IDCARD
            + "?appKey={appKey}&appId={appId}&sign={sign}&idcard={idcard}");

        Map<String, String> kv = new HashMap<String, String>();
        kv.put("idcard", String.valueOf(idcard));

        String resultStr = get(sb.toString(), kv, setting);

        if (null != resultStr) {
            JSONObject jo = new JSONObject(resultStr);
            Integer code = jo.getInt("code");
            if (code == 0) {
                Object object = jo.get("data");
                if (null != object && !object.toString().equals("null")) {
                    JSONObject data = jo.getJSONObject("data");
                    Gson gson = new GsonBuilder().setDateFormat(com.ybkj.daijia.constant.DateConstant.DATE_TIME_FORMATE).create();
                    passenger = gson.fromJson(data.toString(), Passenger.class);
                    result.setResult(passenger);
                }
                result.setSuccess(true);
            } else {
                result.setSuccess(false);
                result.setMessage(jo.getString("message"));
            }
        }
        return result;

    }

    public Result<PassengerAddress> findPassengerToPlace(Long passengerId, Integer type,
        Setting setting) {

        Result<PassengerAddress> result = new Result<PassengerAddress>();
        result.setSuccess(false);

        PassengerAddress passengerAddress = null;

        StringBuilder sb = new StringBuilder();
        String api = PassengerApi.PASSENGER_TOPLACE;
        api = api.replaceAll("\\{passengerId\\}", String.valueOf(passengerId));
        sb.append(getPassengerBaseURL() + api + "?appKey={appKey}&appId={appId}&sign={sign}");
        sb.append("&type={type}");

        Map<String, String> kv = new HashMap<String, String>();
        kv.put("type", String.valueOf(type));

        String resultStr = get(sb.toString(), kv, setting);

        if (null != resultStr) {
            JSONObject jo = new JSONObject(resultStr);
            Integer code = jo.getInt("code");
            if (code == 0) {
                Object object = jo.get("data");
                if (null != object && !object.toString().equals("null")) {
                    JSONObject data = jo.getJSONObject("data");
                    Gson gson = new GsonBuilder().setDateFormat(com.ybkj.daijia.constant.DateConstant.DATE_TIME_FORMATE).create();
                    passengerAddress = gson.fromJson(data.toString(), PassengerAddress.class);
                    result.setResult(passengerAddress);
                }
                result.setSuccess(true);
            } else {
                result.setSuccess(false);
                result.setMessage(jo.getString("message"));
            }
        }
        return result;

    }

    public Result<PassengerGrade> findOneGrade(Long id, Setting setting) {

        Result<PassengerGrade> result = new Result<PassengerGrade>();
        result.setSuccess(false);

        PassengerGrade passengerGrade = null;

        StringBuilder sb = new StringBuilder();
        sb.append(getPassengerBaseURL() + PassengerApi.ONE_PASSENGER_GRADE + id
            + "?appKey={appKey}&appId={appId}&sign={sign}");

        Map<String, String> kv = new HashMap<String, String>();

        String resultStr = get(sb.toString(), kv, setting);

        try {
            if (null != resultStr) {
                JSONObject jo = new JSONObject(resultStr);
                Integer code = jo.getInt("code");
                if (code == 0) {
                    Object object = jo.get("data");
                    if (null != object && !object.toString().equals("null")) {
                        JSONObject data = jo.getJSONObject("data");
                        Gson gson = new GsonBuilder().setDateFormat(com.ybkj.daijia.constant.DateConstant.DATE_TIME_FORMATE).create();
                        passengerGrade = gson.fromJson(data.toString(), PassengerGrade.class);
                        result.setResult(passengerGrade);
                    }
                    result.setSuccess(true);
                } else {
                    result.setSuccess(false);
                    result.setMessage(jo.getString("message"));
                }
            }
        } catch (JsonSyntaxException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return result;

    }

    public Result<PassengerAddress> addPassengerToPlace(Long passengerId,
        String address, Double latitude, Double longitude, Integer type,
        Setting setting) {

        Result<PassengerAddress> result = new Result<PassengerAddress>();
        result.setSuccess(false);

        StringBuilder sb = new StringBuilder();
        String api = PassengerApi.CREATE_PASSENGER_TOPLACE;
        api = api.replaceAll("\\{passengerId\\}",
            String.valueOf(passengerId));
        sb.append(getPassengerBaseURL() + api + "?appKey={appKey}&appId={appId}&sign={sign}");
        sb.append("&address={address}");
        sb.append("&latitude={latitude}");
        sb.append("&longitude={longitude}");
        sb.append("&type={type}");

        Map<String, String> kv = new HashMap<String, String>();
        kv.put("address", String.valueOf(address));
        kv.put("latitude", String.valueOf(latitude));
        kv.put("longitude", String.valueOf(longitude));
        kv.put("type", String.valueOf(type));

        String resultStr = post(sb.toString(), kv, setting);

        if (null != resultStr) {
            JSONObject jo = new JSONObject(resultStr);
            Integer code = jo.getInt("code");
            if (code == 0) {
                JSONObject data = jo.getJSONObject("data");
                Gson gson = new GsonBuilder().setDateFormat(com.ybkj.daijia.constant.DateConstant.DATE_TIME_FORMATE).create();
                PassengerAddress passengerAddress = gson.fromJson(
                    data.toString(), PassengerAddress.class);

                result.setResult(passengerAddress);
                result.setSuccess(true);
            } else {
                String message = jo.getString("message");
                result.setMessage(message);
                result.setSuccess(false);
            }
        }
        return result;

    }

    public Result<PassengerAddress> updatePassengerToPlace(Long passengerId,
        Long addressId, String address, Double latitude, Double longitude,
        Integer type, Setting setting) {

        Result<PassengerAddress> result = new Result<PassengerAddress>();
        result.setSuccess(false);

        StringBuilder sb = new StringBuilder();
        String api = PassengerApi.UPDATE_PASSENGER_TOPLACE;
        api = api.replaceAll("\\{passengerId\\}",
            String.valueOf(passengerId)).replaceAll(
            "\\{addressId\\}", String.valueOf(addressId));
        sb.append(getPassengerBaseURL() + api
            + "?appKey={appKey}&appId={appId}&sign={sign}");
        sb.append("&address={address}");
        sb.append("&latitude={latitude}");
        sb.append("&longitude={longitude}");
        sb.append("&type={type}");

        Map<String, String> kv = new HashMap<String, String>();
        kv.put("address", String.valueOf(address));
        kv.put("latitude", String.valueOf(latitude));
        kv.put("longitude", String.valueOf(longitude));
        kv.put("type", String.valueOf(type));

        String resultStr = put(sb.toString(), kv, setting);

        if (null != resultStr) {
            JSONObject jo = new JSONObject(resultStr);
            String message = jo.getString("message");
            result.setMessage(message);
            result.setSuccess(false);
        } else {
            result.setSuccess(true);
        }

        return result;

    }

    public Result<Passenger> updatePassengerFromPlace(Long passengerId,
        String fromPlace, Double longitude, Double latitude, Setting setting) {

        Result<Passenger> result = new Result<Passenger>();
        result.setSuccess(false);

        StringBuilder sb = new StringBuilder();
        String api = PassengerApi.UPDATE_PASSENGER_FROMPLACE;
        api = api.replaceAll("\\{passengerId\\}", String.valueOf(passengerId));
        sb.append(getPassengerBaseURL() + api
            + "?appKey={appKey}&appId={appId}&sign={sign}");
        sb.append("&address={address}");
        sb.append("&latitude={latitude}");
        sb.append("&longitude={longitude}");

        Map<String, String> kv = new HashMap<String, String>();
        kv.put("address", String.valueOf(fromPlace));
        kv.put("latitude", String.valueOf(latitude));
        kv.put("longitude", String.valueOf(longitude));

        String resultStr = put(sb.toString(), kv, setting);

        if (null != resultStr) {
            JSONObject jo = new JSONObject(resultStr);
            String message = jo.getString("message");
            result.setMessage(message);
            result.setSuccess(false);
        } else {
            result.setSuccess(true);
        }

        return result;

    }

    public Result<PassengerConcats> findPassengerConcats(Long passengerId, Setting setting) {

        Result<PassengerConcats> result = new Result<PassengerConcats>();
        result.setSuccess(false);

        List<PassengerConcats> list = null;

        StringBuilder sb = new StringBuilder();
        String api = PassengerApi.PASSENGER_CONCATS;
        api = api.replaceAll("\\{passengerId\\}", String.valueOf(passengerId));
        sb.append(getPassengerBaseURL() + api + "?appKey={appKey}&appId={appId}&sign={sign}");

        Map<String, String> kv = new HashMap<String, String>();

        String resultStr = get(sb.toString(), kv, setting);

        if (null != resultStr) {
            JSONObject jo = new JSONObject(resultStr);
            Integer code = jo.getInt("code");
            if (code == 0) {
                Object object = jo.get("data");
                if (null != object && !object.toString().equals("[]")) {
                    JSONArray data = jo.getJSONArray("data");
                    Gson gson = new GsonBuilder().setDateFormat(com.ybkj.daijia.constant.DateConstant.DATE_TIME_FORMATE).create();
                    list = gson.fromJson(data.toString(), new TypeToken<List<PassengerConcats>>() {
                    }.getType());
                    result.setData(list);
                }
                result.setSuccess(true);
            } else {
                result.setSuccess(false);
                result.setMessage(jo.getString("message"));
            }
        }
        return result;

    }

    public Result<PassengerConcats> addPassengerConcats(Passenger passenger,
        String memberEcn, String memberEcp, Setting setting) {

        Result<PassengerConcats> result = new Result<PassengerConcats>();
        result.setSuccess(false);

        StringBuilder sb = new StringBuilder();
        String api = PassengerApi.CREATE_PASSENGER_CONCATS;
        api = api.replaceAll("\\{passengerId\\}",
            String.valueOf(passenger.getId()));
        sb.append(getPassengerBaseURL() + api
            + "?appKey={appKey}&appId={appId}&sign={sign}");
        sb.append("&name={name}");
        sb.append("&phone={phone}");

        Map<String, String> kv = new HashMap<String, String>();
        kv.put("name", String.valueOf(memberEcn));
        kv.put("phone", String.valueOf(memberEcp));

        String resultStr = post(sb.toString(), kv, setting);

        if (null != resultStr) {
            JSONObject jo = new JSONObject(resultStr);
            Integer code = jo.getInt("code");
            if (code == 0) {
                JSONObject data = jo.getJSONObject("data");
                Gson gson = new GsonBuilder().setDateFormat(com.ybkj.daijia.constant.DateConstant.DATE_TIME_FORMATE).create();
                PassengerConcats passengerConcats = gson.fromJson(
                    data.toString(), PassengerConcats.class);

                result.setResult(passengerConcats);
                result.setSuccess(true);
            } else {
                String message = jo.getString("message");
                result.setMessage(message);
                result.setSuccess(false);
            }
        }
        return result;

    }

    public Result<PassengerConcats> updatePassengerConcats(Passenger passenger,
        Long concatsId, String memberEcn, String memberEcp, Setting setting) {

        Result<PassengerConcats> result = new Result<PassengerConcats>();
        result.setSuccess(false);

        StringBuilder sb = new StringBuilder();
        String api = PassengerApi.UPDATE_PASSENGER_CONCATS;
        api = api.replaceAll("\\{passengerId\\}",
            String.valueOf(passenger.getId())).replaceAll(
            "\\{concatsId\\}", String.valueOf(concatsId));
        sb.append(getPassengerBaseURL() + api
            + "?appKey={appKey}&appId={appId}&sign={sign}");
        sb.append("&name={name}");
        sb.append("&phone={phone}");

        Map<String, String> kv = new HashMap<String, String>();
        kv.put("name", String.valueOf(memberEcn));
        kv.put("phone", String.valueOf(memberEcp));

        String resultStr = put(sb.toString(), kv, setting);

        if (null != resultStr) {
            JSONObject jo = new JSONObject(resultStr);
            String message = jo.getString("message");
            result.setMessage(message);
            result.setSuccess(false);
        } else {
            result.setSuccess(true);
        }

        return result;

    }

    public Result<PassengerAccount> createPassengerAccount(Passenger passenger, Long introducerId,
        BigDecimal money, PayType payType, String operating,
        String details, Long orderId, String orderNo, Integer orderType, String tradeNo,
        Boolean updateBlance, Boolean useNoDrawMoney, Setting setting) {
        Result<PassengerAccount> result = new Result<PassengerAccount>();
        result.setSuccess(false);

        // 增加财务记录
        StringBuilder sb = new StringBuilder();

        String api = PassengerApi.CREATE_PASSENGER_ACCOUNT;
        api = api.replaceAll("\\{passengerId\\}", String.valueOf(passenger.getId()));
        sb.append(getPassengerBaseURL() + api + "?appKey={appKey}&appId={appId}&sign={sign}");

        sb.append("&money={money}");
        sb.append("&payType={payType}");
        sb.append("&operator={operator}");

        Map<String, String> kv = new HashMap<String, String>();
        kv.put("money", String.valueOf(money));
        kv.put("payType", String.valueOf(payType));
        kv.put("operator", String.valueOf(operating));

        if (null != introducerId) {
            sb.append("&introducerId={introducerId}");
            kv.put("introducerId", String.valueOf(introducerId));
        }

        if (StringUtils.isNotBlank(details)) {
            sb.append("&details={details}");
            kv.put("details", String.valueOf(details));
        }

        if (null != orderId && null != orderNo && null != orderType) {
            sb.append("&orderId={orderId}");
            sb.append("&orderNo={orderNo}");
            sb.append("&orderType={orderType}");

            kv.put("orderId", String.valueOf(orderId));
            kv.put("orderNo", String.valueOf(orderNo));
            kv.put("orderType", String.valueOf(orderType));
        }

        if (StringUtils.isNotBlank(tradeNo)) {
            sb.append("&tradeNo={tradeNo}");
            kv.put("tradeNo", String.valueOf(tradeNo));
        }

        sb.append("&updateBlance={updateBlance}");
        kv.put("updateBlance", String.valueOf(updateBlance));

        sb.append("&useNoDrawMoney={useNoDrawMoney}");
        kv.put("useNoDrawMoney", String.valueOf(useNoDrawMoney));

        String resultStr = post(sb.toString(), kv, setting);

        if (null != resultStr) {
            JSONObject jo = new JSONObject(resultStr);
            Integer code = jo.getInt("code");
            if (code == 0) {
                JSONObject data = jo.getJSONObject("data");
                Gson gson = new GsonBuilder().setDateFormat(com.ybkj.daijia.constant.DateConstant.DATE_TIME_FORMATE).create();
                PassengerAccount passengerAccount = gson
                    .fromJson(data.toString(), PassengerAccount.class);

                result.setResult(passengerAccount);
                result.setSuccess(true);
            } else {
                String message = jo.getString("message");
                result.setMessage(message);
            }
        }

        return result;
    }


    public Result<PassengerAccount> createEnterpriseAccount(
        Enterprise enterprise, BigDecimal money, PayType payType,
        String operating, String details, Long orderId, String orderNo, Integer orderType,
        String tradeNo,
        Boolean updateBlance, Boolean useNoDrawMoney, Setting setting) {
        Result<PassengerAccount> result = new Result<PassengerAccount>();
        result.setSuccess(false);

        // 增加财务记录
        StringBuilder sb = new StringBuilder();

        Map<String, String> kv = new HashMap<String, String>();

        String api = PassengerApi.CREATE_ENTERPRISE_ACCOUNT;
        api = api.replaceAll("\\{enterpriseId\\}",
            String.valueOf(enterprise.getId()));
        sb.append(getPassengerBaseURL() + api
            + "?appKey={appKey}&appId={appId}&sign={sign}");

        sb.append("&money={money}");
        sb.append("&payType={payType}");
        sb.append("&operator={operator}");

        kv = new HashMap<String, String>();
        kv.put("money", String.valueOf(money));
        kv.put("payType", String.valueOf(payType));
        kv.put("operator", String.valueOf(operating));

        if (StringUtils.isNotBlank(details)) {
            sb.append("&details={details}");
            kv.put("details", String.valueOf(details));
        }

        if (null != orderId && null != orderNo && null != orderType) {
            sb.append("&orderId={orderId}");
            sb.append("&orderNo={orderNo}");
            sb.append("&orderType={orderType}");

            kv.put("orderId", String.valueOf(orderId));
            kv.put("orderNo", String.valueOf(orderNo));
            kv.put("orderType", String.valueOf(orderType));
        }

        if (StringUtils.isNotBlank(tradeNo)) {
            sb.append("&tradeNo={tradeNo}");
            kv.put("tradeNo", String.valueOf(tradeNo));
        }

        sb.append("&updateBlance={updateBlance}");
        kv.put("updateBlance", String.valueOf(updateBlance));

        sb.append("&useNoDrawMoney={useNoDrawMoney}");
        kv.put("useNoDrawMoney", String.valueOf(useNoDrawMoney));

        String resultStr = post(sb.toString(), kv, setting);

        if (null != resultStr) {
            JSONObject jo = new JSONObject(resultStr);
            Integer code = jo.getInt("code");
            if (code == 0) {
                JSONObject data = jo.getJSONObject("data");
                Gson gson = new GsonBuilder().setDateFormat(com.ybkj.daijia.constant.DateConstant.DATE_TIME_FORMATE).create();
                PassengerAccount passengerAccount = gson.fromJson(
                    data.toString(), PassengerAccount.class);

                result.setResult(passengerAccount);
                result.setSuccess(true);
            } else {
                String message = jo.getString("message");
                result.setMessage(message);
            }
        }

        return result;
    }

    public Result<PassengerAccount> createPaotuiPassengerAccount(Passenger passenger,
        BigDecimal money, PayType payType, String operating,
        String details, com.ybkj.daijia.server.errand.Order order, Boolean updateBlance,
        Setting setting) {
        Result<PassengerAccount> result = new Result<PassengerAccount>();
        result.setSuccess(false);

        // 增加财务记录
        StringBuilder sb = new StringBuilder();

        Map<String, String> kv = new HashMap<String, String>();

        String api = PassengerApi.CREATE_PASSENGER_ACCOUNT;
        api = api.replaceAll("\\{passengerId\\}", String.valueOf(passenger.getId()));
        sb.append(getPassengerBaseURL() + api + ".json?appKey={appKey}&appId={appId}&sign={sign}");

        sb.append("&money={money}");
        sb.append("&payType={payType}");
        sb.append("&operator={operator}");

        kv = new HashMap<String, String>();
        kv.put("money", String.valueOf(money));
        kv.put("payType", String.valueOf(payType));
        kv.put("operator", operating);

        if (StringUtils.isNotBlank(details)) {
            sb.append("&details={details}");
            kv.put("details", details);
        }

        if (null != order) {
            sb.append("&orderNo={orderNo}");
            sb.append("&orderId={orderId}");
            sb.append("&orderType={orderType}");

            kv.put("orderNo", order.getOrderNo());
            kv.put("orderId", String.valueOf(order.getId()));
            kv.put("orderType", String.valueOf(PassengerAccount.ORDER_TYPE_PAOTUI));

        }

        sb.append("&updateBlance={updateBlance}");
        kv.put("updateBlance", String.valueOf(updateBlance));

        String resultStr = post(sb.toString(), kv, setting);

        if (null != resultStr) {
            JSONObject jo = new JSONObject(resultStr);
            Integer code = jo.getInt("code");
            if (code == 0) {
                JSONObject data = jo.getJSONObject("data");
                Gson gson = new GsonBuilder().setDateFormat(com.ybkj.daijia.constant.DateConstant.DATE_TIME_FORMATE).create();
                PassengerAccount passengerAccount = gson
                    .fromJson(data.toString(), PassengerAccount.class);

                result.setResult(passengerAccount);
                result.setSuccess(true);
            } else {
                String message = jo.getString("message");
                result.setMessage(message);
            }
        }

        return result;
    }

    public List<Passenger> findPassenger(Principal principal, String carNumber, String name,
        String phone, Long[] companyIds, String startTime, String endTime,
        Long[] gradeIds, Integer[] type, Boolean[] allowOverdraw, String[] channelName,
        Integer page, Integer rows) {

        Date start = null;
        Date end = null;

        if (!StringUtils.isEmpty(startTime)) {
            try {
                start = DateUtils.parseDate(startTime, "yyyy-MM-dd");
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(start);
                calendar.set(Calendar.HOUR_OF_DAY, 0);
                calendar.set(Calendar.MINUTE, 0);
                calendar.set(Calendar.SECOND, 0);

                start = calendar.getTime();
            } catch (ParseException e) {
                e.printStackTrace();
            }

        }

        if (!StringUtils.isBlank(endTime)) {
            try {
                end = DateUtils.parseDate(endTime, "yyyy-MM-dd");

                Calendar calendar = Calendar.getInstance();
                calendar.setTime(end);
                calendar.set(Calendar.HOUR_OF_DAY, 23);
                calendar.set(Calendar.MINUTE, 59);
                calendar.set(Calendar.SECOND, 59);

                end = calendar.getTime();
            } catch (ParseException e) {
                e.printStackTrace();
            }

        }

        StringBuilder sb = new StringBuilder();
        sb.append(getPassengerBaseURL() + PassengerApi.QUERY_PASSENGER_BY_MULTICONDITION
            + "?appKey={appKey}&appId={appId}&sign={sign}");

        Map<String, String> kv = new HashMap<String, String>();

        if (!principal.isCompanyType()) {
            kv.put("companyId", String.valueOf(principal.getCompanyId()));
            sb.append("&companyId={companyId}");
        }

        if (null != type && type.length != 0) {
            kv.put("type", Arrays.toString(type).replace("[", "").replace("]", ""));
            sb.append("&type={type}");
        }
        if (null != gradeIds && gradeIds.length != 0) {
            kv.put("gradeId", Arrays.toString(gradeIds).replace("[", "").replace("]", ""));
            sb.append("&gradeId={gradeId}");
        }
        if (null != allowOverdraw && allowOverdraw.length != 0) {
            kv.put("permission", Arrays.toString(allowOverdraw).replace("[", "").replace("]", ""));
            sb.append("&permission={permission}");
        }
        if (null != companyIds && companyIds.length != 0) {
            kv.put("companyIds", Arrays.toString(companyIds).replace("[", "").replace("]", ""));
            sb.append("&companyIds={companyIds}");
        }
        if (null != channelName && channelName.length != 0) {
            kv.put("channelName", Arrays.toString(channelName).replace("[", "").replace("]", ""));
            sb.append("&channelName={channelName}");
        }
        if (null != start) {
            kv.put("start", String.valueOf(start.getTime()));
            sb.append("&start={start}");
        }
        if (null != end) {
            kv.put("end", String.valueOf(end.getTime()));
            sb.append("&end={end}");
        }
        if (StringUtils.isNotBlank(phone)) {
            kv.put("phone", phone);
            sb.append("&phone={phone}");
        }
        if (StringUtils.isNotBlank(name)) {
            kv.put("name", name);
            sb.append("&name={name}");
        }
        if (StringUtils.isNotBlank(carNumber)) {
            kv.put("carNumber", carNumber);
            sb.append("&carNumber={carNumber}");
        }
        if (page != null) {
            kv.put("page", String.valueOf(page));
            sb.append("&page={page}");
        }
        if (rows != null) {
            kv.put("rows", String.valueOf(rows));
            sb.append("&rows={rows}");
        }

        String result = get(sb.toString(), kv, settingUtils.get());

        List<Passenger> passengerList = new ArrayList<Passenger>();

        if (StringUtils.isNotBlank(result)) {
            JSONObject jo = new JSONObject(result);
            Integer code = jo.getInt("code");
            if (code == 0) {
                JSONObject data = jo.getJSONObject("data");
                JSONArray ja = data.getJSONArray("rows");
                if (null != ja && null != ja.toString() && !"[]".equals(ja.toString())) {
                    Gson gson = new GsonBuilder().setDateFormat(com.ybkj.daijia.constant.DateConstant.DATE_TIME_FORMATE).create();
                    passengerList = gson.fromJson(ja.toString(), new TypeToken<List<Passenger>>() {
                    }.getType());
                }
            }
        }
        return passengerList;

    }

    public Long countPassenger(Principal principal, String carNumber, String name,
        String phone, Long[] companyIds, String startTime, String endTime,
        Long[] gradeIds, Integer[] type, Boolean[] allowOverdraw, String[] channelName) {

        Date start = null;
        Date end = null;

        if (!StringUtils.isEmpty(startTime)) {
            try {
                start = DateUtils.parseDate(startTime, "yyyy-MM-dd");
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(start);
                calendar.set(Calendar.HOUR_OF_DAY, 0);
                calendar.set(Calendar.MINUTE, 0);
                calendar.set(Calendar.SECOND, 0);

                start = calendar.getTime();
            } catch (ParseException e) {
                e.printStackTrace();
            }

        }
        if (!StringUtils.isBlank(endTime)) {
            try {
                end = DateUtils.parseDate(endTime, "yyyy-MM-dd");

                Calendar calendar = Calendar.getInstance();
                calendar.setTime(end);
                calendar.set(Calendar.HOUR_OF_DAY, 23);
                calendar.set(Calendar.MINUTE, 59);
                calendar.set(Calendar.SECOND, 59);

                end = calendar.getTime();
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        StringBuilder sb = new StringBuilder();
        sb.append(getPassengerBaseURL() + PassengerApi.COUNT_PASSENGER_BY_MULTICONDITION
            + "?appKey={appKey}&appId={appId}&sign={sign}");
        Map<String, String> kv = new HashMap<String, String>();

        if (!principal.isCompanyType()) {
            kv.put("companyId", String.valueOf(principal.getCompanyId()));
            sb.append("&companyId={companyId}");
        }

        if (null != type && type.length != 0) {
            kv.put("type", Arrays.toString(type).replace("[", "").replace("]", ""));
            sb.append("&type={type}");
        }
        if (null != gradeIds && gradeIds.length != 0) {
            kv.put("gradeId", Arrays.toString(gradeIds).replace("[", "").replace("]", ""));
            sb.append("&gradeId={gradeId}");
        }
        if (null != allowOverdraw && allowOverdraw.length != 0) {
            kv.put("permission", Arrays.toString(allowOverdraw).replace("[", "").replace("]", ""));
            sb.append("&permission={permission}");
        }
        if (null != companyIds && companyIds.length != 0) {
            kv.put("companyIds", Arrays.toString(companyIds).replace("[", "").replace("]", ""));
            sb.append("&companyIds={companyIds}");
        }
        if (null != channelName && channelName.length != 0) {
            kv.put("channelName", Arrays.toString(channelName).replace("[", "").replace("]", ""));
            sb.append("&channelName={channelName}");
        }
        if (null != start) {
            kv.put("start", String.valueOf(start.getTime()));
            sb.append("&start={start}");
        }
        if (null != end) {
            kv.put("end", String.valueOf(end.getTime()));
            sb.append("&end={end}");
        }
        if (StringUtils.isNotBlank(phone)) {
            kv.put("phone", phone);
            sb.append("&phone={phone}");
        }
        if (StringUtils.isNotBlank(name)) {
            kv.put("name", name);
            sb.append("&name={name}");
        }
        if (StringUtils.isNotBlank(carNumber)) {
            kv.put("carNumber", carNumber);
            sb.append("&carNumber={carNumber}");
        }

        String result = get(sb.toString(), kv, settingUtils.get());
        Long count = 0L;
        if (StringUtils.isNotBlank(result)) {
            JSONObject jo = new JSONObject(result);
            Integer code = jo.getInt("code");
            if (code == 0) {
                count = jo.getLong("data");
            }
        }
        return count;
    }

    public Result<PassengerAccount> createPaotuiEnterpriseAccount(
        Enterprise enterprise, BigDecimal money, PayType payType,
        String operating, String details, com.ybkj.daijia.server.errand.Order order,
        Boolean updateBlance, Setting setting) {

        Result<PassengerAccount> result = new Result<PassengerAccount>();
        result.setSuccess(false);

        // 增加财务记录
        StringBuilder sb = new StringBuilder();

        Map<String, String> kv = new HashMap<String, String>();

        String api = PassengerApi.CREATE_ENTERPRISE_ACCOUNT;
        api = api.replaceAll("\\{enterpriseId\\}", String.valueOf(enterprise.getId()));
        sb.append(getPassengerBaseURL() + api + "?appKey={appKey}&appId={appId}&sign={sign}");

        sb.append("&money={money}");
        sb.append("&payType={payType}");
        sb.append("&operator={operator}");

        kv = new HashMap<String, String>();
        kv.put("money", String.valueOf(money));
        kv.put("payType", String.valueOf(payType));
        kv.put("operator", String.valueOf(operating));

        if (StringUtils.isNotBlank(details)) {
            sb.append("&details={details}");
            kv.put("details", String.valueOf(details));
        }

        if (null != order) {
            sb.append("&orderNo={orderNo}");
            sb.append("&orderId={orderId}");
            sb.append("&orderType={orderType}");

            kv.put("orderNo", String.valueOf(order.getOrderNo()));
            kv.put("orderId", String.valueOf(order.getId()));
            kv.put("orderType", String.valueOf(PassengerAccount.ORDER_TYPE_PAOTUI));
        }

        sb.append("&updateBlance={updateBlance}");
        kv.put("updateBlance", String.valueOf(updateBlance));

        String resultStr = post(sb.toString(), kv, setting);

        if (null != resultStr) {
            JSONObject jo = new JSONObject(resultStr);
            Integer code = jo.getInt("code");
            if (code == 0) {
                JSONObject data = jo.getJSONObject("data");
                Gson gson = new GsonBuilder().setDateFormat(com.ybkj.daijia.constant.DateConstant.DATE_TIME_FORMATE).create();
                PassengerAccount passengerAccount = gson
                    .fromJson(data.toString(), PassengerAccount.class);

                result.setResult(passengerAccount);
                result.setSuccess(true);
            } else {
                String message = jo.getString("message");
                result.setMessage(message);
            }
        }

        return result;
    }

    public Result<Receipt> createReceipt(Passenger passenger, BigDecimal money, Setting setting) {
        Result<Receipt> result = new Result<Receipt>();

        StringBuilder sb = new StringBuilder();

        Map<String, String> kv = new HashMap<String, String>();

        String api = PassengerApi.UPDATE_PASSENGER_RECEIPT;
        api = api.replaceAll("\\{passengerId\\}", String.valueOf(passenger.getId()));
        sb.append(getPassengerBaseURL() + api + "?appKey={appKey}&appId={appId}&sign={sign}");

        sb.append("&receiptMoney={receiptMoney}");

        kv = new HashMap<String, String>();
        kv.put("receiptMoney", String.valueOf(money));

        String resultStr = put(sb.toString(), kv, setting);

        if (null != resultStr) {
            JSONObject jo = new JSONObject(resultStr);
            String message = jo.getString("message");
            result.setMessage(message);
            result.setSuccess(false);
        } else {
            result.setSuccess(true);
        }

        return result;
    }

    public BigDecimal sumPassengerAccountMoney(Long companyId,
        Long passengerId, Long introducerId, Date start, Date end, String param,
        String payType, String different, Setting setting) {

        StringBuilder sb = new StringBuilder();

        Map<String, String> kv = new HashMap<String, String>();

        sb.append(getPassengerBaseURL() + PassengerApi.SUM_PASSENGER_ACCOUNT_MONEY
            + "?appKey={appKey}&appId={appId}&sign={sign}");

        kv = new HashMap<String, String>();

        if (null != companyId) {
            sb.append("&companyId={companyId}");
            kv.put("companyId", String.valueOf(companyId));
        }

        if (null != passengerId) {
            sb.append("&passengerId={passengerId}");
            kv.put("passengerId", String.valueOf(passengerId));
        }

        if (null != introducerId) {
            sb.append("&introducerId={introducerId}");
            kv.put("introducerId", String.valueOf(introducerId));
        }

        if (null != start) {
            sb.append("&start={start}");
            kv.put("start", String.valueOf(start.getTime()));
        }

        if (null != end) {
            sb.append("&end={end}");
            kv.put("end", String.valueOf(end.getTime()));
        }

        if (StringUtils.isNotBlank(payType)) {
            String newPayType = "";
            String[] split = payType.split(",");
            for (int i = 0; i < split.length; i++) {
                if (i < split.length - 1) {
                    newPayType += "'" + split[i] + "'" + ",";
                } else {
                    newPayType += "'" + split[i] + "'";
                }
            }
            sb.append("&payType={payType}");
            kv.put("payType", String.valueOf(newPayType));
        }

        if (StringUtils.isNotBlank(param)) {
            sb.append("&param={param}");
            kv.put("param", String.valueOf(param));
        }

        if (StringUtils.isNotBlank(different)) {
            sb.append("&different={different}");
            kv.put("different", String.valueOf(different));
        }

        String resultStr = get(sb.toString(), kv, setting);

        Double total = 0D;
        if (null != resultStr) {
            JSONObject jo = new JSONObject(resultStr);
            Integer code = jo.getInt("code");
            if (code == 0) {
                total = jo.getDouble("data");
            }
        }

        return new BigDecimal(total);
    }

    public BigDecimal sumPassengerVirtual(Long companyId, String param,
        Setting setting) {

        StringBuilder sb = new StringBuilder();

        Map<String, String> kv = new HashMap<String, String>();

        sb.append(getPassengerBaseURL() + PassengerApi.SUM_PASSENGER_VIRTUAL
            + "?appKey={appKey}&appId={appId}&sign={sign}");

        kv = new HashMap<String, String>();

        if (null != companyId) {
            sb.append("&companyId={companyId}");
            kv.put("companyId", String.valueOf(companyId));
        }

        if (StringUtils.isNotBlank(param)) {
            sb.append("&passengerName={passengerName}");
            kv.put("passengerName", String.valueOf(param));
        }

        String resultStr = get(sb.toString(), kv, setting);

        Double total = 0D;
        if (null != resultStr) {
            JSONObject jo = new JSONObject(resultStr);
            Integer code = jo.getInt("code");
            if (code == 0) {
                total = jo.getDouble("data");
            }
        }

        return new BigDecimal(total);
    }

    public Long countPassengerByCompany(Long companyId, Date start, Date end,
        Setting setting) {
        Result<PassengerAccount> result = new Result<PassengerAccount>();
        result.setSuccess(false);

        // 增加财务记录
        StringBuilder sb = new StringBuilder();

        Map<String, String> kv = new HashMap<String, String>();

        sb.append(getPassengerBaseURL() + PassengerApi.COUNT_PASSENGER
            + "?appKey={appKey}&appId={appId}&sign={sign}");

        kv = new HashMap<String, String>();

        if (null != companyId) {
            sb.append("&companyId={companyId}");
            kv.put("companyId", String.valueOf(companyId));
        }

        if (null != start) {
            sb.append("&start={start}");
            kv.put("start", String.valueOf(start.getTime()));
        }

        if (null != end) {
            sb.append("&end={end}");
            kv.put("end", String.valueOf(end.getTime()));
        }

        String resultStr = get(sb.toString(), kv, setting);

        Long total = 0L;
        if (null != resultStr) {
            JSONObject jo = new JSONObject(resultStr);
            Integer code = jo.getInt("code");
            if (code == 0) {
                total = jo.getLong("data");

                result.setSuccess(true);
            } else {
                result.setSuccess(false);
                result.setMessage(jo.getString("message"));
            }
        }

        return total;
    }

    public Result<PassengerGrade> createGrade(PassengerGrade passengerGrade, Setting setting) {

        Result<PassengerGrade> result = new Result<PassengerGrade>();
        result.setSuccess(false);

        StringBuilder sb = new StringBuilder();
        Map<String, String> kv = new HashMap<String, String>();

        sb.append(getPassengerBaseURL() + PassengerApi.CREATE_GRADE
            + ".json?appKey={appKey}&appId={appId}&sign={sign}");

        sb.append("&name={name}");
        kv.put("name", String.valueOf(passengerGrade.getName()));
        if (null != passengerGrade.getCompanyId()) {
            sb.append("&companyId={companyId}");
            kv.put("companyId", String.valueOf(passengerGrade.getCompanyId()));
        }
        if (StringUtils.isNotBlank(passengerGrade.getCompanyName())) {
            sb.append("&companyName={companyName}");
            kv.put("companyName", String.valueOf(passengerGrade.getCompanyName()));
        }
        if (passengerGrade.isDefaultGrade()) {
            sb.append("&defaultGrade={defaultGrade}");
            kv.put("defaultGrade", String.valueOf(passengerGrade.isDefaultGrade()));
        }
        if (null != passengerGrade.getLimitAmounts()) {
            sb.append("&limitAmounts={limitAmounts}");
            kv.put("limitAmounts", String.valueOf(passengerGrade.getLimitAmounts()));
        }
        if (null != passengerGrade.getParallelOrderNumber()) {
            sb.append("&parallelOrderNumber={parallelOrderNumber}");
            kv.put("parallelOrderNumber", String.valueOf(passengerGrade.getParallelOrderNumber()));
        }
        if (null != passengerGrade.getDaijiaAreaId()) {
            sb.append("&daijiaAreaId={daijiaAreaId}");
            kv.put("daijiaAreaId", String.valueOf(passengerGrade.getDaijiaAreaId()));
        }

        if (StringUtils.isNotBlank(passengerGrade.getCarTypes())) {
            sb.append("&carTypes={carTypes}");
            kv.put("carTypes", passengerGrade.getCarTypes());
        }

        if (null != passengerGrade.getTzAreaId()) {
            sb.append("&tzAreaId={tzAreaId}");
            kv.put("tzAreaId", String.valueOf(passengerGrade.getTzAreaId()));
        }
        if (null != passengerGrade.getBrzAreaId()) {
            sb.append("&brzAreaId={brzAreaId}");
            kv.put("brzAreaId", String.valueOf(passengerGrade.getBrzAreaId()));
        }
        if (null != passengerGrade.getRzAreaId()) {
            sb.append("&rzAreaId={rzAreaId}");
            kv.put("rzAreaId", String.valueOf(passengerGrade.getRzAreaId()));
        }
        if (null != passengerGrade.getJjAreaId()) {
            sb.append("&jjAreaId={jjAreaId}");
            kv.put("jjAreaId", String.valueOf(passengerGrade.getJjAreaId()));
        }
        if (null != passengerGrade.getSjAreaId()) {
            sb.append("&sjAreaId={sjAreaId}");
            kv.put("sjAreaId", String.valueOf(passengerGrade.getSjAreaId()));
        }
        if (null != passengerGrade.getJzAreaId()) {
            sb.append("&jzAreaId={jzAreaId}");
            kv.put("jzAreaId", String.valueOf(passengerGrade.getJzAreaId()));
        }
        if (null != passengerGrade.getSzAreaId()) {
            sb.append("&szAreaId={szAreaId}");
            kv.put("szAreaId", String.valueOf(passengerGrade.getSzAreaId()));
        }

        String resultStr = post(sb.toString(), kv, setting);
        if (null != resultStr) {
            JSONObject jo = new JSONObject(resultStr);
            Integer code = jo.getInt("code");
            if (code == 0) {
                JSONObject data = jo.getJSONObject("data");
                Gson gson = new GsonBuilder().setDateFormat(com.ybkj.daijia.constant.DateConstant.DATE_TIME_FORMATE).create();
                PassengerGrade pg = gson.fromJson(data.toString(), PassengerGrade.class);

                result.setResult(pg);
                result.setSuccess(true);
            } else {
                String message = jo.getString("message");
                result.setMessage(message);
                result.setSuccess(false);
            }
        }
        return result;
    }

    public Result<PassengerGrade> updateGrade(PassengerGrade passengerGrade, Setting setting) {

        Result<PassengerGrade> result = new Result<PassengerGrade>();
        result.setSuccess(false);

        StringBuilder sb = new StringBuilder();
        Map<String, String> kv = new HashMap<String, String>();

        String api = PassengerApi.UPDATE_GRADE;
        api = api.replaceAll("\\{gradeId\\}", String.valueOf(passengerGrade.getId()));
        sb.append(getPassengerBaseURL() + api + "?appKey={appKey}&appId={appId}&sign={sign}");

        sb.append("&name={name}");
        kv.put("name", String.valueOf(passengerGrade.getName()));
        if (passengerGrade.isDefaultGrade()) {
            sb.append("&defaultGrade={defaultGrade}");
            kv.put("defaultGrade", String.valueOf(passengerGrade.isDefaultGrade()));
        }
        if (null != passengerGrade.getLimitAmounts()) {
            sb.append("&limitAmounts={limitAmounts}");
            kv.put("limitAmounts", String.valueOf(passengerGrade.getLimitAmounts()));
        }

        if (null != passengerGrade.getParallelOrderNumber()) {
            sb.append("&parallelOrderNumber={parallelOrderNumber}");
            kv.put("parallelOrderNumber", String.valueOf(passengerGrade.getParallelOrderNumber()));
        }

        if (null != passengerGrade.getDaijiaAreaId()) {
            sb.append("&daijiaAreaId={daijiaAreaId}");
            kv.put("daijiaAreaId", String.valueOf(passengerGrade.getDaijiaAreaId()));
        }
        if (StringUtils.isNotBlank(passengerGrade.getCarTypes())) {
            sb.append("&carTypes={carTypes}");
            kv.put("carTypes", passengerGrade.getCarTypes());
        }

        if (null != passengerGrade.getTzAreaId()) {
            sb.append("&tzAreaId={tzAreaId}");
            kv.put("tzAreaId", String.valueOf(passengerGrade.getTzAreaId()));
        }
        if (null != passengerGrade.getBrzAreaId()) {
            sb.append("&brzAreaId={brzAreaId}");
            kv.put("brzAreaId", String.valueOf(passengerGrade.getBrzAreaId()));
        }
        if (null != passengerGrade.getRzAreaId()) {
            sb.append("&rzAreaId={rzAreaId}");
            kv.put("rzAreaId", String.valueOf(passengerGrade.getRzAreaId()));
        }
        if (null != passengerGrade.getJjAreaId()) {
            sb.append("&jjAreaId={jjAreaId}");
            kv.put("jjAreaId", String.valueOf(passengerGrade.getJjAreaId()));
        }
        if (null != passengerGrade.getSjAreaId()) {
            sb.append("&sjAreaId={sjAreaId}");
            kv.put("sjAreaId", String.valueOf(passengerGrade.getSjAreaId()));
        }
        if (null != passengerGrade.getJzAreaId()) {
            sb.append("&jzAreaId={jzAreaId}");
            kv.put("jzAreaId", String.valueOf(passengerGrade.getJzAreaId()));
        }
        if (null != passengerGrade.getSzAreaId()) {
            sb.append("&szAreaId={szAreaId}");
            kv.put("szAreaId", String.valueOf(passengerGrade.getSzAreaId()));
        }

        String resultStr = put(sb.toString(), kv, setting);

        if (null != resultStr) {
            JSONObject jo = new JSONObject(resultStr);
            String message = jo.getString("message");
            result.setMessage(message);
            result.setSuccess(false);
        } else {
            result.setSuccess(true);
        }

        return result;
    }

    public String getPassengerBaseURL() {

        return passengerBaseURL.endsWith("/") ? passengerBaseURL : passengerBaseURL + "/";
    }

    public void setPassengerBaseURL(String passengerBaseURL) {
        this.passengerBaseURL = passengerBaseURL;
    }

    public String getPassengerAppKey() {

        return passengerAppKey;
    }

    public void setPassengerAppKey(String passengerAppKey) {
        this.passengerAppKey = passengerAppKey;
    }

    public String getPassengerAppId() {
        return passengerAppId;
    }

    public void setPassengerAppId(String passengerAppId) {
        this.passengerAppId = passengerAppId;
    }


    public boolean deleteGrade(Long gradeId, Setting setting) {

        StringBuilder sb = new StringBuilder();
        String api = PassengerApi.DELETE_GRADE;
        api = api.replaceAll("\\{gradeId\\}", String.valueOf(gradeId));
        sb.append(getPassengerBaseURL() + api + "?appKey={appKey}&appId={appId}&sign={sign}");

        Map<String, String> kv = new HashMap<String, String>();

        String resultStr = delete(sb.toString(), kv, setting);
        if (StringUtils.isBlank(resultStr)) {
            return true;
        } else {
            return false;
        }

    }

    public boolean disposeGrade(Long gradeId, Setting setting) {

        StringBuilder sb = new StringBuilder();
        String api = PassengerApi.DISPOSE_GRADE;
        api = api.replaceAll("\\{gradeId\\}", String.valueOf(gradeId));
        sb.append(getPassengerBaseURL() + api + "?appKey={appKey}&appId={appId}&sign={sign}");

        Map<String, String> kv = new HashMap<String, String>();

        String resultStr = post(sb.toString(), kv, setting);
        if (StringUtils.isNotBlank(resultStr)) {
            JSONObject jo = new JSONObject(resultStr);
            Integer code = jo.getInt("code");
            if (code == 0) {
                return true;
            }
        }

        return false;
    }


    public Result<PassengerAccount> createPaotuiUpPassengerAccount(Passenger passenger,
        BigDecimal money, PayType payType, String operating,
        String details, com.ybkj.daijia.server.errand.OrderUp order, Boolean updateBlance,
        Setting setting) {
        Result<PassengerAccount> result = new Result<PassengerAccount>();
        result.setSuccess(false);

        // 增加财务记录
        StringBuilder sb = new StringBuilder();

        Map<String, String> kv = new HashMap<String, String>();

        String api = PassengerApi.CREATE_PASSENGER_ACCOUNT;
        api = api.replaceAll("\\{passengerId\\}", String.valueOf(passenger.getId()));
        sb.append(getPassengerBaseURL() + api + ".json?appKey={appKey}&appId={appId}&sign={sign}");

        sb.append("&money={money}");
        sb.append("&payType={payType}");
        sb.append("&operator={operator}");

        kv = new HashMap<String, String>();
        kv.put("money", String.valueOf(money));
        kv.put("payType", String.valueOf(payType));
        kv.put("operator", operating);

        if (StringUtils.isNotBlank(details)) {
            sb.append("&details={details}");
            kv.put("details", details);
        }

        if (null != order) {
            sb.append("&orderNo={orderNo}");
            sb.append("&orderId={orderId}");
            sb.append("&orderType={orderType}");

            kv.put("orderNo", order.getOrderNo());
            kv.put("orderId", String.valueOf(order.getId()));
            kv.put("orderType", String.valueOf(PassengerAccount.ORDER_TYPE_PAOTUI));

        }

        sb.append("&updateBlance={updateBlance}");
        kv.put("updateBlance", String.valueOf(updateBlance));

        String resultStr = post(sb.toString(), kv, setting);

        if (null != resultStr) {
            JSONObject jo = new JSONObject(resultStr);
            Integer code = jo.getInt("code");
            if (code == 0) {
                JSONObject data = jo.getJSONObject("data");
                Gson gson = new Gson();
                PassengerAccount passengerAccount = gson
                    .fromJson(data.toString(), PassengerAccount.class);

                result.setResult(passengerAccount);
                result.setSuccess(true);
            } else {
                String message = jo.getString("message");
                result.setMessage(message);
            }
        }

        return result;
    }

    public Result<PassengerAccount> createPaotuiUpEnterpriseAccount(
        Enterprise enterprise, BigDecimal money, PayType payType,
        String operating, String details, com.ybkj.daijia.server.errand.OrderUp order,
        Boolean updateBlance, Setting setting) {

        Result<PassengerAccount> result = new Result<PassengerAccount>();
        result.setSuccess(false);

        // 增加财务记录
        StringBuilder sb = new StringBuilder();

        Map<String, String> kv = new HashMap<String, String>();

        String api = PassengerApi.CREATE_ENTERPRISE_ACCOUNT;
        api = api.replaceAll("\\{enterpriseId\\}", String.valueOf(enterprise.getId()));
        sb.append(getPassengerBaseURL() + api + "?appKey={appKey}&appId={appId}&sign={sign}");

        sb.append("&money={money}");
        sb.append("&payType={payType}");
        sb.append("&operator={operator}");

        kv = new HashMap<String, String>();
        kv.put("money", String.valueOf(money));
        kv.put("payType", String.valueOf(payType));
        kv.put("operator", String.valueOf(operating));

        if (StringUtils.isNotBlank(details)) {
            sb.append("&details={details}");
            kv.put("details", String.valueOf(details));
        }

        if (null != order) {
            sb.append("&orderNo={orderNo}");
            sb.append("&orderId={orderId}");
            sb.append("&orderType={orderType}");

            kv.put("orderNo", String.valueOf(order.getOrderNo()));
            kv.put("orderId", String.valueOf(order.getId()));
            kv.put("orderType", String.valueOf(PassengerAccount.ORDER_TYPE_PAOTUI));
        }

        sb.append("&updateBlance={updateBlance}");
        kv.put("updateBlance", String.valueOf(updateBlance));

        String resultStr = post(sb.toString(), kv, setting);

        if (null != resultStr) {
            JSONObject jo = new JSONObject(resultStr);
            Integer code = jo.getInt("code");
            if (code == 0) {
                JSONObject data = jo.getJSONObject("data");
                Gson gson = new Gson();
                PassengerAccount passengerAccount = gson
                    .fromJson(data.toString(), PassengerAccount.class);

                result.setResult(passengerAccount);
                result.setSuccess(true);
            } else {
                String message = jo.getString("message");
                result.setMessage(message);
            }
        }

        return result;
    }
}
